var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var nf = deviceone.sm("do_Notification");
var barcode = ui("do_BarcodeView");
var http = require("helper/http");
var external = deviceone.sm("do_External");
var userUrl = require("url/user");
var circleUrl = require("url/circle");
var communityUrl = require("url/community");
var orderUrl = require("url/order");

page.on("back", function() {
	app.closePage();
});
page.on("loaded", function() {
	barcode.start(function(data, e) {
        if(data&&data.value&&data.value!==""){
        	var valueArr = data.value.split("&bizzType=");
        	if(valueArr&&valueArr.length>1){
        		var nextStr = valueArr[valueArr.length-1];
        		var nextArr = nextStr.split("&bizzId=");
        		if(nextArr&&nextArr.length==2){
        			if(nextArr[0]!==""&&nextArr[1]!==""){
        				var bizzType = nextArr[0];
            			var bizzId = nextArr[1];
            			var bizzUrl = userUrl.url.friendApply.face2face;
            			if(bizzType==="A"){
            				bizzUrl = userUrl.url.friendApply.face2face;
            			}else if(bizzType==="B"){
            				bizzUrl = circleUrl.url.member.scan2joinCircle;
            			}else if(bizzType==="C"){
            				bizzUrl = communityUrl.url.join.face2community;
            			}else if(bizzType="ORDER"){
            				bizzUrl = orderUrl.url.base.scan2Finish;//扫码完成自提订单
            			}
            			http.post(bizzUrl,{"bizzId":bizzId},function(data1){
                			if(bizzType==="A"){
                				nf.alert("已成功加为好友", function() {
                					app.closePage();
                			    });
                			}else if(bizzType==="B"){
                				nf.alert("已成功加入圈子", function() {
                					app.closePage();
                			    });
                			}else if(bizzType==="C"){
                				nf.alert("已成功加入社区", function() {
                					app.closePage();
                			    });
                			}else if(bizzType==="ORDER"){
                				nf.alert("扫描成功,买家可以提走商品.", function() {
                					app.closePage();
                			    });
                			}
                    	},function(data2){
                    		if(data2&&data2.msg&&data2.msg!==""){
                    			nf.alert(data2.msg, function() {
                					app.closePage();
                			    });
                    		}else{
                    			app.closePage();
                    		}
                    	});
        			}
        		}
        	}else{
        		var href=data.value;
				if(href.indexOf("http://")>=0){
					external.openURL(href);
				}
        		app.closePage();
        	}
        }else{
    		if(data.value){
				var href=data.value;
				if(href.indexOf("http://")>=0){
					external.openURL(href);
				}
			}
    		app.closePage();
    	}
    });
});
var layoutHead = ui("layoutHead");
var addheader = layoutHead.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
  app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
  "labelTitle" : "二维码"
});
header.refreshData();